package com.tjetc.Impl;

import com.tjetc.domain.Admin;
import com.tjetc.domain.Admins;
import com.tjetc.mapper.AdminsMapper;
import com.tjetc.service.AdminService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminsMapper adminsMapper = Db.getMapper(AdminsMapper.class);
    private Result result = new Result();

    @Override
    public boolean login(String username, String password) {//登录
        Admin adminByName = adminsMapper.getAdminByName(username);
        String password1 = adminByName.getPassword();
        if(adminByName ==null){
            System.out.println("没有发现这个用户");
            return false;
        }else {
            if (password1.equals(password)) {
                return true;
            } else {
                System.out.println("密码错误");
                return false;
            }
        }
    }

    @Override
    public Result register(Admin admin) {
        String username = admin.getUsername();
        if (username.equals("")){
            return result.setMsg("用户名不能为空").setCode(500);
        }
        String password = admin.getPassword();
        if (password.equals("")){
            System.out.println("密码不能为空");
            return result.setMsg("密码不能为空").setCode(500);
        }
        int insert = adminsMapper.insert(admin);
        if (insert==1){
            return result.setMsg("添加成功").setCode(200);
        }
        return result.setMsg("添加失败").setCode(500);
    }

    @Override
    public Result update(Admin admin) {//修改
        String username = admin.getUsername();
        Admin adminByName = adminsMapper.getAdminByName(username);
        Admin admin1 = adminsMapper.selectByPrimaryKey(admin.getId());
        if (admin1==null){
            System.out.println("没有这个用户");
            return result.setCode(500).setMsg("没有这个用户");
        }
        if (adminByName==null){
            int i = adminsMapper.updateByPrimaryKeySelective(admin);
            if (i==1){
                return result.setMsg("修改成功").setCode(200);
            }else return result.setMsg("修改失败").setCode(500);
        }else{
            return result.setMsg("用户名已存在").setCode(500);
        }
    }

    @Override
    public Result delete(int id) {//删除
        Admin admin = adminsMapper.selectByPrimaryKey(id);
        if (admin ==null){
            return result.setMsg("没有发现这个用户").setCode(500);
        }
        int i = adminsMapper.deleteByPrimaryKey(id);
        if (i==1){
            return result.setMsg("删除成功").setCode(200);
        }else return result.setMsg("删除失败").setCode(500);
    }

    @Override
    public List<Admin> getAdminsList() {
        return adminsMapper.getAdminList();
    }

    @Override
    public Admin getAdminsByName(String username) {
        Admin adminByName = adminsMapper.getAdminByName(username);
        if (adminByName==null){
            System.out.println( "没有发现这个用户");
            return null;
        }else return adminByName;
    }


    @Override
    public Result findByNameAP(Admins admins) {
        Admins admins1=adminsMapper.findByName(admins.getUsername());
        while (true){
            if (admins1==null){
                return result.setCode(500).setMsg("该用户不存在");
            }else if (!(admins.getPassword().equals(admins1.getPassword()))){
                return result.setCode(500).setMsg("密码错误请重新输入");
            }
            break;
        }
        return result.setCode(200).setMsg("登录成功").setData(admins1);
    }
}
